package com.example.ccj.b.c.dialog;

import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Bitmap;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;


import com.example.ccj.R;
import com.example.ccj.b.c.draw.SignatureView;
import com.example.ccj.u.ActivityUtils;
import com.example.ccj.u.GenerateId;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.ThreadUtils;
import com.example.ccj.u.ToastUtils;
import com.github.gzuliyujiang.colorpicker.ColorPicker;
import com.github.gzuliyujiang.colorpicker.OnColorPickedListener;
import com.luck.picture.lib.entity.LocalMedia;


/**
 * 画板dialog
 * 清空、选择颜色、画笔大小、保存
 */
public class DrawingBoardDialog extends DialogBase{
    DrawBoarDialogListener drawBoarDialogListener;
    SignatureView signatureView;
    public DrawingBoardDialog(@NonNull Context context, DrawBoarDialogListener drawBoarDialogListener) {
        super(context);
        this.drawBoarDialogListener = drawBoarDialogListener;
    }


    public DrawingBoardDialog(@NonNull Context context) {
        super(context);
    }


    @Override
    public void onInit() {

        setAlpha(0.8f);//透明度
        isShowBackground(false);//不显示背景
        setIsFull(true);//全屏显示
        setCancelable(false);//点击不允许关闭

        setLayout(R.layout.dialog_drawing_boar);
        signatureView = findViewById(R.id.dialog_drawing_boar_view);
        ImageView draw_clear = findViewById(R.id.dialog_drawing_boar_draw_clear);
        ImageView draw_ok = findViewById(R.id.dialog_drawing_boar_draw_ok);
//        ImageView draw_cancel = findViewById(R.id.dialog_drawing_boar_draw_cancel);
        TextView selectColor = findViewById(R.id.dialog_drawing_boar_select_color);
        SeekBar seekBar = findViewById(R.id.drawing_boar_seek_bar);
        signatureView.setWidth(20);

        //清空绘制的手绘重绘
        draw_clear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (drawBoarDialogListener != null) {
                    drawBoarDialogListener.onClear();
                }
                signatureView.clear();
            }
        });

        if (drawBoarDialogListener == null) {
            draw_ok.setVisibility(View.GONE);
        } else {
            //绘制完毕
            //保存位图文件到本地并通知图库更新并保存到图片列表，上传到OSS
            draw_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    LoadingDialog loadDialog2 = new LoadingDialog(ActivityUtils.getTopActivity());
                    loadDialog2.show();

                    ThreadUtils.executeByCpu(new ThreadUtils.SimpleTask<Bitmap>() {
                        @Override
                        public Bitmap doInBackground() throws Throwable {
                            //获取签名位图
                            Bitmap bitmap = signatureView.getSignatureBitmap();
                            //保存图片到本地图库
                            String img = ImageUtils.saveBitMap2File(v.getContext(), bitmap, "img", GenerateId.getIdToStr());
                            //获取文件回调
                            LocalMedia localMedia = LocalMedia.generateLocalMedia(v.getContext(), img);
                            if (drawBoarDialogListener != null) {
                                drawBoarDialogListener.onBitMap(bitmap);
                                drawBoarDialogListener.onFile(localMedia);
                            }
                            return null;
                        }

                        @Override
                        public void onSuccess(Bitmap bitmap) {
                            loadDialog2.dismiss();
                            dismiss();
                        }

                        @Override
                        public void onFail(Throwable t) {
                            ToastUtils.showShort(t.getMessage());
                            loadDialog2.dismiss();
                            super.onFail(t);
                        }
                    });
                }
            });
        }

//        //取消手写
//        draw_cancel.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                if (drawBoarDialogListener != null) {
//                    drawBoarDialogListener.onCancel();
//                }
//                dismiss();
//            }
//        });

        //选择颜色
        selectColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColorPicker colorPicker = new ColorPicker(ActivityUtils.getTopActivity());
                colorPicker.setOnColorPickListener(new OnColorPickedListener() {
                    @Override
                    public void onColorPicked(int pickedColor) {
                        signatureView.setColor(pickedColor);
                        selectColor.setBackgroundColor(pickedColor);
                        // 设置滑块颜色
                        seekBar.setThumbTintList(ColorStateList.valueOf(pickedColor));
                        // 设置进度条颜色
                        seekBar.setProgressTintList(ColorStateList.valueOf(pickedColor));
                    }
                });
                colorPicker.show();
            }
        });

        //设置画笔宽度
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                signatureView.setWidth(progress);
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {}
        });

    }

    /**
     * 回调
     */
    public interface DrawBoarDialogListener{
        void onClear();//清空
        void onFile(LocalMedia localMedia);//返回本地文件
        void onBitMap(Bitmap bitmap);//返回bitmap
        void onCancel();//取消
    }

    /**
     * 重写回退取消
     * 手动回退会执行这个方法
     * 代码里dismiss不会执行这个
     */
    @Override
    public void cancel() {
        super.cancel();
        //用户回退取消绘制时调用oncancel回调
        //如果当前存在回调时使用回调方法处理
        //没有回调询问是否保存草稿到图库
        if (drawBoarDialogListener != null) {
            drawBoarDialogListener.onCancel();
        } else {
            SureOrCancelDialog dialog = new SureOrCancelDialog(getContext());
            dialog.setCenterText("要保存草稿吗？");
            dialog.setButtonSureText("保存");
            dialog.setButtonSureClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    ThreadUtils.executeByCpu(new ThreadUtils.SimpleTask<Bitmap>() {
                        @Override
                        public Bitmap doInBackground() throws Throwable {
                            //获取签名位图
                            return signatureView.getSignatureBitmap();
                        }

                        @Override
                        public void onSuccess(Bitmap bitmap) {
                            ImageUtils.saveBitMap2File(v.getContext(), bitmap, "img", GenerateId.getIdToStr());
                        }
                    });
                }
            });
            dialog.setButtonCancelText("不保存");
            dialog.setButtonCancelClick(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
        }
    }
}
